package com.service.Impl;

import com.entity.*;
import com.exception.GlobalException;
import com.mapper.NotificationMapper;
import com.mapper.PostCommentMapper;
import com.mapper.PostMapper;
import com.mapper.UserMapper;
import com.service.PostCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CommentsDaoImpl
 * Description:
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/04/16 16:25
 */
@Service
public class PostCommentServiceImpl implements PostCommentService {
    @Autowired
    private PostCommentMapper postCommentMapper;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NotificationMapper notificationMapper;

    @Override
    public List<CommentTree> getCommentTreeByPostId(int postId) {
        //获取按 path 排序的评论树
        List<CommentTree> commentTree = postCommentMapper.getCommentTree(postId);

        //构建层级结构
        Map<Integer, CommentTree> parentMap = new HashMap<>();
        List<CommentTree> rootComments = new ArrayList<>();
        for (CommentTree comment : commentTree) {
            parentMap.put(comment.getCommentId(), comment);
            if (comment.getLevel() == 0) {
                rootComments.add(comment);
            } else {
                CommentTree parent = parentMap.get(comment.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(comment);
                }
            }
        }
        return rootComments;
    }

    @Override
    @Transactional
    public void addComment(PostComment comment) {
        try {
            //1.添加评论
            postCommentMapper.addComment(comment);

            //2.查询出评论者的信息
            User ToUser = userMapper.selectUserInfoByAccountId(comment.getAccountId());

            //3.查询出被评论者的信息
            List<Post> postsByPostId = postMapper.getPostsByPostId(comment.getPostId());

            //3. 添加用户评论通知
            if (comment.getToUserId() == 0) {
                notificationMapper.insert(new Notification
                        (null, postsByPostId.get(0).getAccountId(), "用户评论了您的帖子", "用户 "+ToUser.getNickname() + "评论了您的帖子", (byte) 3, false, null));
            } else {
                notificationMapper.insert(new Notification
                        (null, comment.getToUserId(), "用户回复", "用户 "+ToUser.getNickname() + "回复了你", (byte) 3, false, null));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(500, "添加评论失败");
        }
    }

    @Override
    public int getCommentCountByPostId(int postId) {
        try {
            return postCommentMapper.countCommentsByPostId(postId);
        } catch (Exception e) {
            throw new GlobalException(500, "获取评论数量失败");
        }
    }
}
